unit UnDataOpslag;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, SvcMgr, Dialogs,
  IdBaseComponent, IdComponent, IdUDPBase, IdUDPServer, IdSocketHandle,
  UndataModel,UnDmDatasets;

type
  TTransportPackage = array[0..19] of byte;

type
  TSrvDataOpslag = class(TService)
    DataOpslag: TIdUDPServer;
    procedure DataOpslagUDPRead(AThread: TIdUDPListenerThread; AData: TBytes;
      ABinding: TIdSocketHandle);
  private
    procedure ProcessConfig(var AData: TBytes);
    procedure ProcessData(var AData: TBytes);
  public
    function GetServiceController: TServiceController; override;
    { Public declarations }
  end;

var
  SrvDataOpslag: TSrvDataOpslag;

implementation

{$R *.DFM}

procedure ServiceController(CtrlCode: DWord); stdcall;
begin
  SrvDataOpslag.Controller(CtrlCode);
end;

procedure TSrvDataOpslag.ProcessConfig(var AData: TBytes);
var
  aMeting : TMeting;
begin
  aMeting := TMeting.create((AData[2] * 256) + AData[3],DmDatasets);
  AData[0] := 2;
  if aMeting.Module.ModuleId > 0 then
  begin
    if aMeting.Module.Configuratie.CongiguratieId > 0 then
    begin
      aMeting.Module.Energrie := (AData[4] * 256) + AData[5];
      aMeting.Module.Save;
      with aMeting.Module.Configuratie do
      begin
        IncreaseSequence;
        AData[4] := CongiguratieId;
        AData[5] := 0;
        // sensor 1
        AData[6] := round(Interval1 / $10);
        AData[7] := ((Interval1 mod 8) * 8) + SensorType1;
        // sensor 1
        AData[8] := round(Interval2 / $10);
        AData[9] := ((Interval2 mod 8) * 8) + SensorType2;
        // sensor 1
        AData[10] := round(Interval3 / $10);
        AData[11] := ((Interval3 mod 8) * 8) + SensorType3;
        // sensor 1
        AData[12] := round(Interval4 / $10);
        AData[13] := ((Interval4 mod 8) * 8) + SensorType4;
        // sensor 1
        AData[14] := round(Interval5 / $10);
        AData[15] := ((Interval5 mod 8) * 8) + SensorType5;
        // sensor 1
        AData[16] := round(Interval6 / $10);
        AData[17] := ((Interval6 mod 8) * 8) + SensorType6;
        //
      end;
    end;
  end;
end;

procedure TSrvDataOpslag.ProcessData(var AData: TBytes);
var
  aMeting : TMeting;
  MeetWaarde : integer;
begin
  aMeting := TMeting.create((AData[2] * 256) + AData[3],DmDatasets);
  AData[0] := 2;
  if AData[4] = aMeting.module.Configuratie.CongiguratieId then
  begin
    MeetWaarde := (AData[5] * 256) + AData[6];
    if MeetWaarde < 32768  then
    sleep(0);
    MeetWaarde := (AData[7] * 256) + AData[8];
    if MeetWaarde < 32768  then
    sleep(0);
    MeetWaarde := (AData[9] * 256) + AData[10];
    if MeetWaarde < 32768  then
    sleep(0);
    MeetWaarde := (AData[11] * 256) + AData[12];
    if MeetWaarde < 32768  then
    sleep(0);
    MeetWaarde := (AData[13] * 256) + AData[14];
    if MeetWaarde < 32768  then
    sleep(0);
    MeetWaarde := (AData[15] * 256) + AData[16];
  end;
end;

procedure TSrvDataOpslag.DataOpslagUDPRead(AThread: TIdUDPListenerThread;
  AData: TBytes; ABinding: TIdSocketHandle);
var
  DataBericht : TTransportPackage;
begin
  if SizeOf(AData) = 20 then
  begin
    AData[1] := AData[1] + 1;
    AData[1] := AData[1] mod 100;
    case AData[0] of
      0 : ProcessConfig(AData);
      1 : ProcessData(AData);
    end;
  end;
  DataOpslag.SendBuffer(aBinding.PeerIP,ABinding.PeerPort,ABinding.IPVersion,AData);
end;

function TSrvDataOpslag.GetServiceController: TServiceController;
begin
  Result := ServiceController;
end;

end.
